﻿using Core.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Core.Infarastructure;
using StackAttack_WindowsFormApp.Helpers;


namespace StackAttack_WindowsFormApp.Animation
{
    class DestroyingAnimation
    {
        private Timer animateExplosion;
        private Graphics _gr;
        private List<Point> pointsOfExplosion;
        private List<Point> lineExplosionPoints;
        private List<Bitmap> spritesOfExplosion;
        MainApp app;
        Point currentPoint;
        public bool LineExplosion { get; set; }
        private int currentFrame = 0;
        private int quantityOfAnimatedBlocks = 0;
        private int currentBlock = 0;


        public DestroyingAnimation(MainApp e)
        {
            app = e;
            Initialize(e.DrawingBox.CreateGraphics());
        }
        

        public void StartAnimation(Point pointOfExplosion)
        {
            quantityOfAnimatedBlocks = 1;
            currentPoint = pointOfExplosion;
            animateExplosion.Start();
        }

        public void StartAnimationForLine()
        {
            quantityOfAnimatedBlocks = Constants.STACKS_COUNT_IN_GORIZONTAL_LINE;

            animateExplosion.Start();
        }
        private void animateExplosion_Tick(object sender, EventArgs e)
        {
            if (currentBlock < quantityOfAnimatedBlocks)
            {
                if (LineExplosion)
                {
                    currentPoint = lineExplosionPoints[currentBlock];
                    
                }
                
                
                if (currentFrame == spritesOfExplosion.Count)
                {
                    currentFrame = 0;
                    currentBlock++;
                    return;
                    
                }
                _gr.DrawImage(spritesOfExplosion[currentFrame], currentPoint);
               // app.DrawingBox.Refresh();
                app.DrawingBox.Update();
                currentFrame++;
                

                
            }
            else
            {
                animateExplosion.Stop();
                quantityOfAnimatedBlocks = 0;
                currentBlock = 0;
                pointsOfExplosion.Clear();
                return;
            }
            
        }

        private void Initialize(Graphics e)
        {
            pointsOfExplosion = new List<Point>();
            animateExplosion = new Timer();
            animateExplosion.Interval = 30;
            animateExplosion.Tick += animateExplosion_Tick;
            _gr = e;




            Bitmap stackExplosion = new Bitmap(PathFinder.GetFullPathToFile("Images\\grenades2.png"));
            spritesOfExplosion = new List<Bitmap>();
            int framesCount = 6;
            int currX = 0;

            int _frameHeight = stackExplosion.Height;
            int _frameWidth = stackExplosion.Width / framesCount;
            while (currX < stackExplosion.Width - _frameWidth)
            {
                Rectangle rect = new Rectangle(currX, 0, _frameWidth, _frameHeight);
                spritesOfExplosion.Add(stackExplosion.Clone(rect, stackExplosion.PixelFormat));
                currX += _frameWidth;
            }

            lineExplosionPoints = new List<Point>();
            for (int i = 0; i < Constants.DRAWING_FIELD_WIDTH; i += Constants.STACK_SIZE)
            {
                lineExplosionPoints.Add(new Point(i, Constants.DRAWING_FIELD_HEIGHT - Constants.STACK_SIZE));
            }
        }
        
    }
}
